package com.ruoyi.web.controller.campus;

import java.util.List;
import java.util.UUID;

import com.ruoyi.common.utils.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.campus.domain.MyUserInfo;
import com.ruoyi.campus.service.IMyUserInfoService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 用户基本信息Controller
 *
 * @author zht
 * @date 2021-10-24
 */
@RestController
@RequestMapping("/campus/userInfo")
public class MyUserInfoController extends BaseController
{
    @Autowired
    private IMyUserInfoService myUserInfoService;

    /**
     * 查询用户基本信息列表
     */
    @PreAuthorize("@ss.hasPermi('campus:userInfo:list')")
    @GetMapping("/list")
    public TableDataInfo list(MyUserInfo myUserInfo)
    {
        startPage();
        List<MyUserInfo> list = myUserInfoService.selectMyUserInfoList(myUserInfo);
        return getDataTable(list);
    }

    /**
     * 导出用户基本信息列表
     */
    @PreAuthorize("@ss.hasPermi('campus:userInfo:export')")
    @Log(title = "用户基本信息", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(MyUserInfo myUserInfo)
    {
        List<MyUserInfo> list = myUserInfoService.selectMyUserInfoList(myUserInfo);
        ExcelUtil<MyUserInfo> util = new ExcelUtil<MyUserInfo>(MyUserInfo.class);
        return util.exportExcel(list, "用户基本信息数据");
    }

    /**
     * 获取用户基本信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('campus:userInfo:query')")
    @GetMapping(value = "/{infoId}")
    public AjaxResult getInfo(@PathVariable("infoId") String infoId)
    {
        return AjaxResult.success(myUserInfoService.selectMyUserInfoByInfoId(infoId));
    }

    /**
     * 新增用户基本信息
     */
    @PreAuthorize("@ss.hasPermi('campus:userInfo:add')")
    @Log(title = "用户基本信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MyUserInfo myUserInfo)
    {
        myUserInfo.setInfoId(UUID.randomUUID().toString().replace("-",""));
        return toAjax(myUserInfoService.insertMyUserInfo(myUserInfo));
    }

    /**
     * 修改用户基本信息
     */
    @PreAuthorize("@ss.hasPermi('campus:userInfo:edit')")
    @Log(title = "用户基本信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MyUserInfo myUserInfo)
    {
        if (StringUtils.isNotEmpty(myUserInfo.getNumber())
                && "1".equals(myUserInfoService.checkNumberExist(myUserInfo)))
        {
            return AjaxResult.error("修改详细资料失败，学号已存在");
        }
        if(myUserInfoService.updateMyUserInfo(myUserInfo) > 0){
            return AjaxResult.success();
        }
        return AjaxResult.error("修改详细资料异常，请联系管理员");
    }

    /**
     * 删除用户基本信息
     */
    @PreAuthorize("@ss.hasPermi('campus:userInfo:remove')")
    @Log(title = "用户基本信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{infoIds}")
    public AjaxResult remove(@PathVariable String[] infoIds)
    {
        return toAjax(myUserInfoService.deleteMyUserInfoByInfoIds(infoIds));
    }
}
